package com.cyx.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cyx.reggie.common.BaseContext;
import com.cyx.reggie.common.R;
import com.cyx.reggie.entity.ShoppingCart;
import com.cyx.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 陈彦西
 * @date 2024/7/19 下午5:22
 */
@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;
    /**
     * 添加购物车
     */
    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart) {
       log.info("购物车数据：{}", shoppingCart);
        //设置用户id,指定当前是哪个用户的购物车
        Long currentId= BaseContext.getCurrentId();
         shoppingCart.setUserId(currentId);

        //查询当前菜品或者套餐是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getDishId, dishId);

        if(dishId!=null){
         //添加到购物车的是菜品
        queryWrapper.eq(ShoppingCart::getDishId, dishId);
        }else{
            //添加到购物车的是套餐
        queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        //SQL:SELECT * FROM shopping_cart where user_id =? and disah_id/setmeal_id =?
        ShoppingCart  CartServiceOne = shoppingCartService.getOne(queryWrapper);
        // 已经有了菜品就在原来数量基础加1
        if(CartServiceOne!=null){
            //如果不存在就添加到购物车，数量默认为1
            Integer number = CartServiceOne.getNumber();
            CartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(CartServiceOne);
        }else{
            //如果不存在，则添加到购物车，数量默认为1
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            CartServiceOne =  shoppingCart;
        }


        return R.success(CartServiceOne);

    }

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        log.info("查看购物车.....");
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
            queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    /**
     * 清空购物车
     */
    @DeleteMapping("/clean")
    public R<String> clean() {
        //SQL:delete from shopping_cart where user_id =?
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }



    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart) {
        // 设置用户id
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        // 查询当前菜品或套餐是否在数据表中
        Long dishId = shoppingCart.getDishId();// 获取菜品id
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, currentId);
        if (dishId != null) {
            // 说明添加的是菜品
            queryWrapper.eq(ShoppingCart::getDishId, dishId);
        }else {
            // 添加购物车的是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);// 查询当前菜品或套餐是否在数据表中

        if (cartServiceOne != null && cartServiceOne.getNumber() > 1) {
            // 如果存在，在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number - 1);
            shoppingCartService.updateById(cartServiceOne);

        }else {

            shoppingCartService.removeById(cartServiceOne.getId());
            cartServiceOne.setNumber(0);
        }
        return R.success(cartServiceOne);
    }

//    /**
//     * 删除菜品
//     */
//    @DeleteMapping("/sub")
//    public R<String> sub(@RequestParam("id") Long id) {
//
//        //查询当前菜品或者套餐是否在购物车中
//        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getDishId, id);
//
//        if(id!=null){
//            //添加到购物车的是菜品
//            queryWrapper.eq(ShoppingCart::getDishId, id);
//        }else{
//            //添加到购物车的是套餐
//            queryWrapper.eq(ShoppingCart::getSetmealId, id);
//        }
//        //SQL:SELECT * FROM shopping_cart where user_id =? and disah_id/setmeal_id =?
//        ShoppingCart  CartServiceOne = shoppingCartService.getOne(queryWrapper);
//        // 已经有了菜品就在原来数量基础加1
//        if(CartServiceOne!=null){
//            //如果不存在就添加到购物车，数量默认为1
//            Integer number = CartServiceOne.getNumber();
//            CartServiceOne.setNumber(number+1);
//            shoppingCartService.updateById(CartServiceOne);
//        }else{
//            //如果不存在，则添加到购物车，数量默认为1
//            shoppingCart.setNumber(1);
//            shoppingCartService.save(shoppingCart);
//            CartServiceOne =  shoppingCart;
//        }
//        //SQL:delete from shopping_cart where user_id =?
//        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
//        shoppingCartService.remove(queryWrapper);
//        return R.success("清空购物车成功");
//    }

}

